// $codepro.audit.disable
/* Copyright (c) 2011 Team A CS2340. All rights reserved.*/

package edu.gatech.teama.doctorsoffice.client.view;


import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.JLabel;
import net.miginfocom.swing.MigLayout;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

import java.awt.Font;
import javax.swing.JButton;


import edu.gatech.teama.doctorsoffice.client.presenter.PatientInstructionsPresenter;
import edu.gatech.teama.doctorsoffice.model.PatientInstructions;
import edu.gatech.teama.doctorsoffice.model.TreatmentRecord;
import javax.swing.JScrollPane;

/**
 * PatientInstructionsPanel.
 *
 * @author unknown
 * @version 1.0
 */
public class PatientInstructionsPanel extends JPanel {
	
	/** pharmaName. */
	private JTextField pharmaName;

	/** pillsperday. */
	private JTextField pillsperday;

	/** pillduration. */
	private JTextField pillduration;

	/** record. */
	private TreatmentRecord record; // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unassignedField

	/** the instructions area. */
	private JTextArea addiInstruct;

	/** the text area for followup info. */
	private JTextArea followup;

	/** the text area for other info. */
	private JTextArea other;

	/** myPresenter. */
	private PatientInstructionsPresenter myPresenter;

	/**
	 * patient instructions panel.
	 *
	 * @param treatmentRecord the treatment record
	 */
	public PatientInstructionsPanel(TreatmentRecord treatmentRecord) { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		this.record = treatmentRecord;

		myPresenter = new PatientInstructionsPresenter(record);
		setLayout(new MigLayout("",
				"[][][][][grow]", "[22px][][][][][][25][][20px,grow,top][][][]"));

		JLabel lblTreatmentPlan = new JLabel("Treatment Plan");
		lblTreatmentPlan.setFont(new Font("Tahoma", Font.PLAIN, 18));
		add(lblTreatmentPlan, "cell 0 0 5 1,alignx center,aligny top");

		JLabel lblPrescriptions = new JLabel("Prescriptions:");
		lblPrescriptions.setFont(new Font("Tahoma", Font.BOLD, 12));
		add(lblPrescriptions, "cell 0 1 4 1");

		JLabel lblFollowupInstructions = new JLabel("Follow-Up Instructions:");
		lblFollowupInstructions.setFont(new Font("Tahoma", Font.BOLD, 12));
		add(lblFollowupInstructions, "cell 4 1");

		JLabel lblPharmaceuticalName = new JLabel("Pharmaceutical Name:");
		add(lblPharmaceuticalName, "flowx,cell 1 2 2 1");

		pharmaName = new JTextField();
		add(pharmaName, "cell 1 2 2 1,grow");
		pharmaName.setColumns(10);

		JLabel lblPillsPerDay = new JLabel("Pills Per Day:");
		add(lblPillsPerDay, "flowx,cell 1 3 2 1");

		pillsperday = new JTextField();
		add(pillsperday, "cell 1 3 2 1,grow");
		pillsperday.setColumns(10);

		JLabel lblNewLabel = new JLabel("\r\nDuration:");
		add(lblNewLabel, "flowx,cell 1 4 2 1");

		JLabel lblAdditionalInstructions = new JLabel("Additional Instructions:");
		add(lblAdditionalInstructions, "cell 1 5 2 1");

		JLabel lblOtherInstructions = new JLabel("Other Instructions:");
		lblOtherInstructions.setFont(new Font("Tahoma", Font.BOLD, 12));
		add(lblOtherInstructions, "cell 4 5");

		JScrollPane sPane3 = new JScrollPane();
		addiInstruct = new JTextArea();
		add(sPane3, "cell 1 6 2 1,grow");
		sPane3.setViewportView(addiInstruct);

		JScrollPane sPane2 = new JScrollPane();
		other = new JTextArea();
		add(sPane2, "cell 4 6 1 2,grow");
		sPane2.setViewportView(other);

		JLabel lblNewLabel_1 = new JLabel("Invoice:\r\n\r\n\r\n\r\n");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 12));
		add(lblNewLabel_1, "cell 0 7 2 1");

		pillduration = new JTextField();
		add(pillduration, "cell 1 4 2 1,grow");
		pillduration.setColumns(10);

		JButton btnGenerate = new JButton("Generate");

		add(btnGenerate, "cell 2 7");



		JScrollPane sPane1 = new JScrollPane();
		followup = new JTextArea();
		add(sPane1, "cell 4 2 1 2,grow");
		sPane1.setViewportView(followup);

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, "cell 2 8 1 4,grow");

		final JLabel textArea = new JLabel();
		scrollPane.setViewportView(textArea);
		btnGenerate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textArea.setText(myPresenter.generateInvoice());
			}
		});
		PatientInstructions pi = record.getPatientInstructions();
		if (pi.getPrescriptionList().size() > 0) {
			pharmaName.setText(pi.getPrescriptionList().get(0).getDrugName());
			pillsperday.setText("" + pi.getPrescriptionList().get(0).getTimesPerDay());
			pillduration.setText("" + pi.getPrescriptionList().get(0).getNumberOfPills());
			addiInstruct.setText(pi.getPrescriptionList().get(0).getOtherInstructions());
		}
		followup.setText(pi.getFollowUpInstructions());
		other.setText(pi.getOtherInstructions());
	}

	/**
	 * Save the updated info to the presenter.
	 */
	public void save() {
		myPresenter.save(addiInstruct, 
				followup, other, pharmaName, pillsperday, pillduration);
	}

	/**
	 * Sets the editable regions to true or false based on the user type
	 *
	 * @param result the new editable
	 */
	public void setEditable(boolean result) {
		pharmaName.setEditable(result);
		pillsperday.setEditable(result);
		pillduration.setEditable(result);
		addiInstruct.setEditable(result);
		followup.setEditable(result);
		other.setEditable(result);

	}
}
